package com.techsoul.module.system.controller;

import com.techsoul.common.core.pojo.Result;
import com.techsoul.module.system.pojo.param.MenuSaveParam;
import com.techsoul.module.system.pojo.param.MenuSearchParam;
import com.techsoul.module.system.service.IMenuService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.Arrays;

@Api(tags = "菜单接口")
@RestController
@RequestMapping("/system/menu")
public class MenuController {

    @Autowired
    private Result result;

    @Autowired
    private IMenuService service;

    @ApiOperation("新增")
    @PreAuthorize("hasAnyAuthority('system:menu:insert')")
    @PostMapping
    public Result insert(@Valid @RequestBody MenuSaveParam params) {
        return result.success(this.service.insert(params));
    }

    @ApiOperation("批量删除（多个用逗号隔开）")
    @PreAuthorize("hasAnyAuthority('system:menu:delete')")
    @DeleteMapping("/{ids}")
    public Result batchDelete(@PathVariable String[] ids) {
        return result.success(this.service.batchDelete(Arrays.asList(ids)));
    }

    @ApiOperation("修改")
    @PreAuthorize("hasAnyAuthority('system:menu:update')")
    @PutMapping
    public Result update(@Valid @RequestBody MenuSaveParam params) {
        return result.success(this.service.update(params));
    }

    @ApiOperation("更新树形表信息")
    @PreAuthorize("hasAnyAuthority('system:menu:update')")
    @PutMapping("updateAll")
    public Result updateAll() {
        this.service.updateAll();
        return result.success();
    }

    @ApiOperation("详情")
    @PreAuthorize("hasAnyAuthority('system:menu:search')")
    @GetMapping("/{id}")
    public Result findById(@PathVariable("id") String id) {
        return result.success(this.service.findById(id));
    }

    @ApiOperation("列表")
    @PreAuthorize("hasAnyAuthority('system:menu:search')")
    @PostMapping("/search")
    public Result findAll(@RequestBody MenuSearchParam params) {
        return result.success(this.service.findAll(params));
    }

    @ApiOperation("列表-分页")
    @PreAuthorize("hasAnyAuthority('system:menu:search')")
    @PostMapping("/search/{current}/{pageSize}")
    public Result findAll(@RequestBody MenuSearchParam params, @PathVariable Integer current, @PathVariable Integer pageSize) {
        return result.success(this.service.findAll(params, current, pageSize));
    }

    @ApiOperation("树形结构数据")
    @PreAuthorize("hasAnyAuthority('system:menu:search')")
    @PostMapping("/search/tree")
    public Result findTree(@RequestBody MenuSearchParam params) {
        return result.success(this.service.findTree(params));
    }

    @ApiOperation("树形结构选择器数据")
    @PreAuthorize("hasAnyAuthority('system:menu:search')")
    @PostMapping("/search/treeSelect")
    public Result findTreeSelect(@RequestBody MenuSearchParam params) {
        return result.success(this.service.findTreeSelect(params));
    }

    @ApiOperation("用户菜单列表")
    @PreAuthorize("hasAnyAuthority('system:menu:search')")
    @PostMapping("/search/userMenus")
    public Result findUserMenus() {
        return result.success(this.service.findUserMenus());
    }

    @ApiOperation("角色菜单列表")
    @PreAuthorize("hasAnyAuthority('system:menu:search')")
    @PostMapping("/search/roleMenus")
    public Result findRoleMenus(@RequestBody MenuSearchParam params) {
        return result.success(this.service.findRoleMenus(params.getRoleId()));
    }

}
